<template>
	<view class="container">
		<view class="content">
			<view class="lang">
				<uni-icons custom-prefix="iconfont" type="icon-lang" size="20" @tap="maskShow"></uni-icons>
			</view>
			<view class="header flex_center">
				<image class="logo" src="../../static/img/logo.png" mode=""></image>
			</view>
			<view class="title">
				注册
			</view>
			<view class="form">
				<view class="form_item">
					<view class="form_title">
						邮箱
					</view>
					<view class="input_box flex">
						<input type="text" v-model="form.email" style="flex:1" maxlength="20" />
					</view>
				</view>
				<view class="fixed">
					<view class="done btn flex_between" @tap="toRegister">
						<text>发送验证码</text>
						<uni-icons type="arrow-right" color="#fff" size="24"></uni-icons>
					</view>
				</view>
				<view class="register">
					<text>已有账户，</text> <text style="color: #0065ff;" @tap="toLogin">立即登录</text>
				</view>
			</view>
		</view>
		<u-popup :show="show" @close="closeMask">
			<view class="mask">
				<view class="mask_close flex_center" @tap="closeMask">
					<uni-icons type="closeempty"></uni-icons>
				</view>
				<view class="mask_content">
					<view class="mask_title">选择语言</view>
					<view class="item flex_between" v-for="item in langList" @tap="changeLang(item)">
						<image class="item_logo" :src="item.img" mode=""></image>
						<view class="detail">
							<view class="item_value">{{item.value}}</view>
							<view class="item_title">{{item.title}}</view>
						</view>
						<view class="item_icon flex_center" :class="activeLang==item.value?'active_icon':''">
							<uni-icons custom-prefix="iconfont" type="icon-duigou" size="15"
								:color="activeLang==item.value?'#fff':'#3d3d3d'"></uni-icons>
						</view>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import headerView from "@/components/headerView.vue"
	import {
		sendEmail
	} from "@/api/api.js"
	export default {
		components: {
			headerView
		},
		data() {
			return {
				delShow: false,
				form: {},
				langList: [{
					title: "English",
					value: "en_US",
					img: "#"
				}, {
					title: "简体中文",
					value: "zh_CN",
					img: "#"
				}],
				show: false,
				activeLang: uni.getStorageSync("lang"),
			};
		},
		onLoad() {},
		methods: {
			changeLang(opt) {
				if (this.activeLang == opt.value) return
				uni.setStorageSync('lang', opt.value);
				this.activeLang = opt.value
				setTimeout(() => {
					this.closeMask()
					uni.reLaunch({
						url: '/pages/login/login'
					});
				}, 200);
			},
			maskShow(key) {
				this.show = true
			},
			closeMask() {
				this.show = false
			},
			toLogin() {
				uni.navigateTo({
					url: "/pages/login/login"
				})
			},
			async toRegister() {
				try {
					// 验证邮箱是否为空
					if (!this.form.email) {
						uni.$u.toast('请输入邮箱');
						return;
					}

					// 构建请求参数
					const data = {
						email: this.form.email,
						event: 'reg',
					};

					// 发送邮件请求
					const response = await sendEmail(data);

					// 处理邮件发送成功的逻辑
					// 这里可以根据接口返回的数据进行相应的处理
					console.log(response.data);

					// 这里可以添加一些成功后的提示
					uni.showToast({
						title: response.data.msg,
						icon: 'success',
						duration: 2000,
						success: () => {
							// 跳转到验证码输入页面，传递邮箱参数
							uni.navigateTo({
								url: `/pages/login/sign?email=${this.form.email}&event=register&code=${response.data.data.code}`
							});
						}
					});
				} catch (error) {
					// 处理邮件发送失败的逻辑
					// 这里可以根据接口返回的错误信息进行相应的处理
					console.error(error.data.msg);
					// 如果接口返回了错误信息 (msg)，则显示该错误信息，否则显示通用提示
					const errorMessage = error.data.msg
					uni.$u.toast(errorMessage);
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		min-height: 100vh;
		height: auto;

		.content {
			padding-right: 0;

			.lang {
				display: flex;
				justify-content: flex-end;
				margin-bottom: 30rpx;
				padding-right: 40rpx;
			}

			.header {
				padding-right: 40rpx;

				.logo {
					width: 30vw;
					height: 30vw;
				}
			}

			.title {
				padding-right: 40rpx;
				width: 100%;
				text-align: center;
				font-size: 44rpx;
				margin-top: 4vh;
				font-family: PM;
			}

			.form {
				margin-top: 10rpx;

				.form_item {
					padding: 40rpx 40rpx 0 0;

					.form_title {
						font-family: PM;
						opacity: .8;
						margin: 0 20rpx;

						.forget {
							text-decoration: underline;
							opacity: .5;
						}
					}

					.input_box {
						// width: 100%;
						margin: 20rpx 20rpx 0;
						background-color: hsla(0, 0%, 92.9%, .46);
						border-radius: 20rpx;
						font-size: 30rpx;
						padding: 0 40rpx;
						height: 98rpx;
						line-height: 98rpx;
						border: 4rpx solid #3d3d3d;
						font-weight: 600;
						font-family: PM;
					}
				}

				.fixed {
					margin-top: 8vh;

					.btn {
						height: 100rpx;
						line-height: 100rpx;
						color: #fff;
						font-size: 32rpx;
						padding: 0 40rpx;
						border-radius: 20rpx;
						background-image: url("../../static/img/btn_bg.png");
						background-size: cover;
						background-repeat: no-repeat;
						overflow: hidden;
						// position: absolute;
						// right: 0;
						// left: 0;
						// bottom: 10vh;
					}

					.done {
						background-color: #0065ff;
						margin-left: 40rpx;
						margin-top: 40rpx;
						border-radius: 20rpx 0 0 20rpx;
					}
				}

				.register {
					margin-top: 5vh;
					opacity: .5;
					text-align: center;
					margin-right: 40rpx;
				}
			}
		}

		.mask {
			width: 100%;
			height: 60vh;
			background: #fff;
			padding: 40rpx;
			border-radius: 40rpx 40rpx 0 0;
			color: #333;

			.mask_close {
				width: 72rpx;
				height: 72rpx;
				background-color: hsla(0, 0%, 92.9%, .46);
				border-radius: 20rpx;
			}

			.mask_content {
				.mask_title {
					font-size: 50rpx;
					padding-top: 40rpx;
					font-family: PM;
				}

				.item {
					column-gap: 30rpx;
					margin-top: 36rpx;

					.item_logo {
						width: 90rpx;
						height: 90rpx;
						border-radius: 50%;
						box-shadow: 0px 6rpx 16rpx 0 rgba(0, 0, 0, .16)
					}

					.detail {
						flex: 1;

						.item_value {
							opacity: .5;
						}

						.item_title {
							font-size: 32rpx;
							font-family: PM;
						}
					}

					.item_icon {
						width: 36rpx;
						height: 36rpx;
						opacity: .2;
						border: 2rpx solid #3d3d3d;
						border-radius: 6rpx;
					}

					.active_icon {
						opacity: 1;
						border-color: #0065ff;
						background: #0065ff;
					}
				}

			}
		}

	}
</style>